<template>
	<view class="login">
		<view class="logo">
			<image src="/static/logo.png" mode="aspectFit" class="logo-image" />
		</view>
		<view class="form">
			<button class="button wx-login" @click="wxLogin">
				<image src="/static/start/wx.png" mode="aspectFit" class="wx-icon" />
				<text class="wx-text">微信登录</text>
			</button>
		</view>
	</view>
</template>

<script>
	// 引入vuex
	import {
		mapState
	} from 'vuex';
	import {
		mapMutations
	} from 'vuex'
	export default {
		
		
		methods: {
			 
			wxLogin() {
				var that = this 
				// 调用微信登录接口
				uni.login({
					provider: 'weixin',
					withCredentials: true, // 请求带上登录态信息
					success(res) { 
						uni.setStorageSync("openid", res.openid);
						uni.getUserInfo({
							provider: 'weixin',
							success(res) { 
								// 获取用户头像和昵称
								const avatarUrl = res.userInfo.avatarUrl;
								const nickname = res.userInfo.nickName; 
								// TODO: 将头像和昵称保存到本地或发送给服务器
								uni.setStorageSync('nickname',nickname); 
								uni.downloadFile({
									url: avatarUrl,
									success(res) {
										// 保存成功后，将头像的本地路径保存到本地存储中
										uni.setStorageSync('avatarUrl', res.tempFilePath); 
										// 登录成功，跳转到首页
										uni.switchTab({
											url: '/pages/Index/Index'
										});
									},
									fail(err) {
										console.error(err);
									}
								});
								
							},
							fail(err) {
								console.error(err);
							}
						});

					},
					fail(err) {
						console.error(err);
					}
				});
			}
		}
	}
</script>

<style lang="scss">
	.login {
		display: flex;
		flex-direction: column;
		justify-content: center;
		align-items: center;
		width: 100%;
		height: 100%;

		.logo {
			margin-top: 140rpx;
			margin-bottom: 50rpx;

			.logo-image {
				width: 600rpx;
				height: 600rpx;
			}
		}

		.form {
			margin-top: 46%;
			display: flex;
			flex-direction: column;
			justify-content: center;
			align-items: center;

			.button {
				display: flex;
				justify-content: center;
				align-items: center;
				width: 340rpx;
				height: 90rpx;
				background-color: #1AAD19;
				border-radius: 40rpx;
				margin-bottom: 30rpx;

				.wx-icon {
					width: 40rpx;
					height: 40rpx;
					margin-right: 30rpx;
				}

				.wx-text {
					color: #FFFFFF;
					font-size: 36rpx;
				}
			}
		}
	}
</style>
